/*
 * @(#)IHEUtils.java
 * Date 2013-03-30
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hl7v3common.utils;

import java.io.Serializable;

import java.util.List;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.BL;
import org.hl7.v3.INT;
import org.hl7.v3.IVLTS;
import org.hl7.v3.ObjectFactory;
import org.hl7.v3.TS;

import org.joda.time.DateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.model.common.CodedElement;
import us.hornerscorners.lamppost.model.common.ContactSystem;
import us.hornerscorners.lamppost.model.common.InstanceIdentifier;
import us.hornerscorners.lamppost.model.common.TimeInterval;

import us.hornerscorners.lamppost.utils.UTCDateUtils;

import static us.hornerscorners.lamppost.model.common.ContactSystemType.Email;
import static us.hornerscorners.lamppost.model.common.ContactSystemType.Fax;
import static us.hornerscorners.lamppost.model.common.ContactSystemType.Url;


/**
 *
 * @author jim
 */
public class HL7Utils {

    /** Field description */
    public static final ObjectFactory hl7Factory = new ObjectFactory();

    /** Field description */
    private static final Logger logger = LoggerFactory.getLogger(HL7Utils.class);

    /**
     * Method description
     *
     *
     * @param content
     *
     * @return
     */
    public static String parseFirstSerializable(List<Serializable> content) {

        String result = null;

        if (content.isEmpty() == false) {

            Object obj = content.get(0);

            if (obj instanceof JAXBElement) {

                result = ((JAXBElement) obj).getValue().toString();

            } else {

                result = obj.toString();
            }
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param content
     *
     * @return
     */
    public static String parseFirstString(List<String> content) {

        String result = null;

        if (content.isEmpty() == false) {

            result = content.get(0).toString();
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param tstime
     * @param formats
     *
     * @return
     */
    public static DateTime parseUTCDate(TS tstime, String[] formats) {

        DateTime result = null;

        if (tstime != null) {

            result = UTCDateUtils.parseDate(tstime.getValue(), formats);
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param blboolean
     *
     * @return
     */
    public static Boolean toBoolean(BL blboolean) {

        Boolean result = null;

        if (blboolean != null) {

            result = blboolean.isValue();
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param code
     * @return
     */
    public static String toCEString(CodedElement code) {

        String system = StringUtils.defaultIfBlank(code.getCodeSystem(),
                            code.getCodeSystemName());

        return String.format("%s^^%s", StringUtils.trimToEmpty(code.getCode()),
                             StringUtils.trimToEmpty(system));
    }

    /**
     * Method description
     *
     *
     * @param id
     * @return
     */
    public static String toCXString(InstanceIdentifier id) {

        return String.format("%s^^^&%s&ISO", StringUtils.trimToEmpty(id.getExtension()),
                             StringUtils.trimToEmpty(id.getRoot()));
    }

    /**
     * Method description
     *
     *
     * @param effectiveTime
     *
     * @return
     */
    public static TimeInterval toEffectiveTime(IVLTS effectiveTime) {

        TimeInterval result = null;

        if (effectiveTime != null) {

            // TODO
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param intinteger
     *
     * @return
     */
    public static Integer toInteger(INT intinteger) {

        Integer result = null;

        if (intinteger != null) {

            result = Integer.valueOf(intinteger.getValue().intValue());
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param date
     *
     * @return
     */
    public static String toUTCDateTimeString(DateTime date) {

        return UTCDateUtils.formatDate(date, UTCDateUtils.DATE_TIME_FORMAT);
    }

    /**
     * Method description
     *
     *
     * @param contactSystem
     * @return
     */
    public static String toXTNString(ContactSystem contactSystem) {

        String type = "PH";

        if (contactSystem.getSystemType() != null) {

            switch (contactSystem.getSystemType()) {

                case Email :
                case Url :
                    type = "Internet";

                    break;

                case Fax :
                    type = "FX";

                    break;
            }
        }

        return String.format("^^%s^%s", type,
                             StringUtils.trimToEmpty(contactSystem.getValue()));
    }
}
